package com.cw.subject.domain.handler.subject;

import com.cw.subject.common.enums.IsDeletedFlagEnum;
import com.cw.subject.common.enums.SubjectInfoTypeEnum;
import com.cw.subject.domain.convert.JudgeSubjectConverter;
import com.cw.subject.domain.entity.SubjectAnswerBO;
import com.cw.subject.domain.entity.SubjectInfoBO;
import com.cw.subject.domain.entity.SubjectOptionBO;
import com.cw.subject.infra.basic.entity.SubjectJudgePO;
import com.cw.subject.infra.basic.service.SubjectJudgePOService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 判断题目的策略类
 * 
 * @author: 江传文
 * @date: 2024/4/12 16:50
 */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectJudgePOService subjectJudgePOService;
    
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE; // 只处理JUDGE类型的
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //判断题目的插入
        // 判断题的答案是一对一的关系,无需使用List
        SubjectJudgePO subjectJudgePO = new SubjectJudgePO();
        // 由于OptionList只有一个isCorrect,所以这里直接获取索引为0的元素即可
        SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
        subjectJudgePO.setSubjectId(subjectInfoBO.getId());
        subjectJudgePO.setIsCorrect(subjectAnswerBO.getIsCorrect());
        subjectJudgePO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectJudgePOService.insert(subjectJudgePO);
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectJudgePO subjectJudgePO = new SubjectJudgePO();
        subjectJudgePO.setSubjectId(subjectId);
        List<SubjectJudgePO> result = subjectJudgePOService.queryByCondition(subjectJudgePO);
        List<SubjectAnswerBO> subjectAnswerBOList = JudgeSubjectConverter.INSTANCE.convertEntityToBoList(result);

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }

}
